package com.web.base.wechat.provider;

import com.blue.core.util.BeanUtil;
import com.blue.jdbc.Page;
import com.blue.validation.ValidationUtil;
import com.blue.validation.group.SaveModel;
import com.blue.validation.group.UpdateModel;
import com.web.base.wechat.cache.WechatCache;
import com.web.base.wechat.exception.WechatException;
import com.web.base.wechat.facade.WechatFacade;
import com.web.base.wechat.message.WechatRequest;
import com.web.base.wechat.message.WechatResponse;
import com.web.base.wechat.model.Wechat;
import com.web.base.wechat.service.WechatService;
import com.web.base.wechat.util.WechatConvertor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.function.Function;

@Component
public class WechatProvider implements WechatFacade
{
	@Autowired
	private WechatService wechatService;
	@Autowired
	private WechatCache wechatCache;
	
	public WechatProvider()
	{
	}

	@Override
	public WechatResponse add(WechatRequest request)
	{
		ValidationUtil.valid(request, SaveModel.class);
		return this.process(request, wechatService::save);
	}

	@Override
	public WechatResponse update(WechatRequest request)
	{
		ValidationUtil.valid(request, UpdateModel.class);
		return this.process(request, wechatService::save);
	}
	
	private WechatResponse process(WechatRequest request, Function<Wechat, Wechat> f)
	{
		Wechat wechat = WechatConvertor.toWechat(request);
		wechat = f.apply(wechat);
		wechatCache.delete();
		WechatResponse response = WechatConvertor.toResponse(wechat);
		return response;
	}

	@Override
	public Page<WechatResponse> listPage(WechatRequest request, int start, int size)
	{
		Wechat wechat = WechatConvertor.toWechat(request);
		Page<Wechat> page = new Page<>(size);
		page.setCurPage(start);
		page = wechatService.list(wechat, page);
		Page<WechatResponse> responsePage = WechatConvertor.toResponse(page);
		return responsePage;
	}

	@Override
	public List<WechatResponse> listAll()
	{
		List<Wechat> list = wechatCache.reverseConvert(wechatCache.listAll());
		List<WechatResponse> responseList = WechatConvertor.toResponse(list);
		return responseList;
	}

	@Override
	public WechatResponse get(Integer id)
	{
		if (id == null)
			throw new WechatException("ID不能为空");
		
		Wechat wechat = BeanUtil.createBean(Wechat.class, wechatService.get(id));
		if (wechat == null)
			wechat = wechatService.get(id);

		WechatResponse response = WechatConvertor.toResponse(wechat);
		return response;
	}

	@Override
	public WechatResponse getByCode(String code)
	{
		if (code == null || code.isEmpty())
			throw new WechatException("编号不能为空");
		
		Wechat wechat = BeanUtil.createBean(Wechat.class, wechatService.getByCode(code));
		WechatResponse response = WechatConvertor.toResponse(wechat);
		return response;
	}

	@Override
	public void refresh()
	{
		wechatCache.delete();
	}
}
